/* 
 页面无参使用useShare
 const { onShareAppMessage, onShareTimeline } = useShare()
 页面传参使用useShare
 const { onShareAppMessage, onShareTimeline } = useShare({title:'标题',path:'页面路径'})
 或者
 const { onShareAppMessage, onShareTimeline } = useShare()
 useShare({title:'标题',path:'页面路径'})
 */

const defaultConfig = {
	// 自定义标题,必填
	title: '',
	// 默认跳转首页,必填
	path: `/pages/tab/index/index`,
	// 可设置默认分享图，不设置默认截取头部5:4
	imageUrl: '',
	// 自定义query参数(QQ小程序、微信分享到朋友圈)
	query:''
}

const useShare = (shareConfig = defaultConfig) => {
	const shareInfo = reactive(shareConfig)
	// 判断自定义分享传参是否位空对象
	if((Object.keys(shareInfo).length == 0)) {
		shareInfo = defaultConfig
	} else {
		if(shareInfo.title == undefined) {
			shareInfo.title = defaultConfig.title
		}
		if(shareInfo.path == undefined) {
			shareInfo.path = defaultConfig.path
		}
	}
	// 发送给朋友
	onShareAppMessage((res) => {
		// 来自页面内分享按钮
		if (res.from === 'button') {
			console.log(res)
		}
		// 动态获取当前页面栈
		let pages = getCurrentPages()
		let nowPage = pages[pages.length - 1]
		shareInfo.path = `/${nowPage.route}`
		
		return shareInfo
	})
	// 分享到朋友圈
	// #ifdef MP-WEIXIN
	onShareTimeline((res) => {
		// 来自页面内分享按钮
		if (res.from === 'button') {
			console.log(res.target)
		}
		// 获取所有页面栈实例列表
		let pages = getCurrentPages() 
		// 当前页页面实例
		let nowPage = pages[pages.length - 1] 
		shareInfo.path = `/${nowPage.route}`
		
		return shareInfo
	})
	// #endif
	return { onShareAppMessage, onShareTimeline }
}

export { useShare }